package com.baizhi.cmfz.controller;

import cn.afterturn.easypoi.excel.ExcelExportUtil;
import cn.afterturn.easypoi.excel.entity.ExportParams;
import cn.hutool.core.lang.UUID;
import com.baizhi.cmfz.annotation.LogAnnotation;
import com.baizhi.cmfz.entity.Guru;
import com.baizhi.cmfz.service.GuruService;
import com.baizhi.cmfz.util.FileExportUtils;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.Workbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.*;
import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/guru")
public class GuruController {

	@Autowired
	private GuruService guruService;


	/**
	 *
	 * @param page 前端传递来的参数 表示当前的页码
	 * @param limit 每页显示的行数
	 * @return 以json的形式返回数据
	 */
	@LogAnnotation(value = "查询上师分页信息",type = "select")
	@RequestMapping("/selectGuruPage")
	public Map selectGuruPage(int page,int limit){
		return guruService.selectGuruPage(page, limit);
	}


	/**
	 *
	 * @param guruImg 上传的头像
	 * @param guru 上传的大师信息
	 * @param request 响应对向
	 * @return 返回上传的结果信息
	 * @throws IOException 上传失败抛出异常
	 */
	@LogAnnotation(value = "添加上师的信息",type = "insert")
	@RequestMapping("/insertGuru")
	public Map insertGuru(MultipartFile guruImg, Guru guru, HttpServletRequest request) throws IOException {
//		文件的上传
//		获取文件的名称

		String filename = guruImg.getOriginalFilename();

//		新文件的名字

		String newName = UUID.fastUUID().toString().replace("-", "")+"_"+filename;

//		获取文件存放当路径

		String img = request.getServletContext().getRealPath("img");

		guruImg.transferTo(new File(img+"/"+newName));

		guru.setGuruImage("/img/"+newName);

		return guruService.insertGuru(guru);
	}

	/**
	 *
	 * @param guruId 要修改的大师的信息，大师的头像信息不修改
	 *
	 * @return 返回修改的信息详情
	 */
	@LogAnnotation(value = "修改上师的信息",type = "update")
	@RequestMapping("/updateGuru")
	public Map updateGuru(int guruId,int guruStatus){

		return guruService.updateGuru(guruId,guruStatus);
	}



	@LogAnnotation(value = "查询一位上师的信息",type = "select")
	@RequestMapping("/selectById")
	public Guru selectById(int guruId){
		return guruService.selectById(guruId);
	}



	@LogAnnotation(value = "查询所有的上师",type = "select")
	@RequestMapping("/selectAll")
	public List<Guru> selectAll(){
		return guruService.selectAll();
	}


	@LogAnnotation(value = "批量上传大师数据",type = "update")
	@RequestMapping("/MultiInsert")
	public Map MultiInsert(MultipartFile file,HttpServletRequest request) throws IOException {
		InputStream inputStream1 = file.getInputStream();

//		通过poi处理流
		HSSFWorkbook workbook = new HSSFWorkbook(inputStream1);
//		获得表
		HSSFSheet sheet = workbook.getSheetAt(0);
//		获取表中的行，最后一行的下标
		int rowNum = sheet.getLastRowNum();
		List<Guru> guruList = new ArrayList<>();
		for (int i = 1; i <= rowNum; i++) {
//			获取行
			HSSFRow row = sheet.getRow(i);
//			通过行得到单元格
			String id = row.getCell(0).getStringCellValue();
			String name = row.getCell(1).getStringCellValue();
			String image = row.getCell(2).getStringCellValue();
			String nickName = row.getCell(3).getStringCellValue();
			String status = row.getCell(4).getStringCellValue();
			Guru guru = new Guru();
			guru.setGuruId(Integer.parseInt(id));
			guru.setGuruName(name);
			guru.setGuruNickname(nickName);
			guru.setGuruImage(image);
			guru.setGuruStatus(status);
			guruList.add(guru);
		}
		//将集合中的数据添加到数据库
		return guruService.MultiInsert(guruList);
	}



	@LogAnnotation(value = "导出大师信息到excel表格",type = "export")
	@RequestMapping("/exportGuru")
	public void exportGuru(HttpServletResponse response) throws IOException{
//		将上师的数据全部查询出来
		List<Guru> guruList = guruService.selectAll();
		guruList.forEach(s->s.setGuruImage("http://localhost:9999/cmfzdemo"+s.getGuruImage()));

//		设置想听头信息
		response.setHeader("Content-Disposition", "attachment;fileName=" + URLEncoder.encode("guruCmfz.xls","utf-8"));
		response.setContentType("application/vnd.ms-excel");
		ServletOutputStream outputStream = response.getOutputStream();
		ExportParams exportParams = new ExportParams("上师表", "guru");
		Workbook workbook = ExcelExportUtil.exportExcel(exportParams, Guru.class,guruList);
//		导出文件
		workbook.write(outputStream);
	}
}
